package com.xbb.controller;

import com.xbb.common.RespResult;
import com.xbb.constant.SessionConstant;
import com.xbb.entity.Levels;
import com.xbb.entity.Services;
import com.xbb.entity.User;
import com.xbb.entity.Appointment;
import com.xbb.service.AppointmentService;
import com.xbb.service.LevelsService;
import com.xbb.service.ServicesService;
import com.xbb.service.UserService;
import com.xbb.util.DateUtils;
import com.xbb.util.FileUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 预约订单Controller
 */
@RestController
@CrossOrigin
@RequestMapping("/appointment")
public class AppointmentController {

    @Resource
    private AppointmentService appointmentService;
    @Resource
    private UserService userService;
    @Resource
    private LevelsService levelsService;
    @Resource
    private ServicesService servicesService;

    // 根据条件查询分页数据及条数
    @RequestMapping("/data")
    public RespResult data(@RequestParam("page") int page,
                           @RequestParam("limit") int limit,
                           @RequestParam(value = "servicesId", required = false) Integer servicesId,
                           @RequestParam(value = "userId", required = false) Integer userId,
                           @RequestParam(value = "appointmentDate", required = false) String appointmentDate,
                           @RequestParam(value = "matter", required = false) String matter,
                           @RequestParam(value = "status", required = false) Integer status,
                           @RequestParam(value = "creatDate", required = false) Date creatDate,
                           @RequestParam(value = "staffId", required = false) Integer staffId,
                           @RequestParam(value = "number", required = false) String number,
                           HttpSession session) {
        User user = (User) session.getAttribute(SessionConstant.KEY_USER);
        List<Appointment> appointmentList = appointmentService.selectListByPaging(
                page,
                limit,
                servicesId,
                userId,
                appointmentDate,
                matter,
                status,
                creatDate,
                staffId,
                number
        );
        int count = appointmentService.selectCountByPaging(
                servicesId,
                userId,
                appointmentDate,
                matter,
                status,
                creatDate,
                staffId,
                number
        );
        RespResult respResult = new RespResult();
        respResult.success(appointmentList, count);
        return respResult;
    }

    // 查询全部数据
    @RequestMapping("/all")
    public RespResult all() {
        List<Appointment> appointmentList = appointmentService.selectAll();
        RespResult respResult = new RespResult();
        respResult.success(appointmentList);
        return respResult;
    }

    // 根据条件查询列表数据
    @RequestMapping("/list")
    public RespResult list(@RequestBody Appointment appointment) {
        List<Appointment> appointmentList = appointmentService.selectList(appointment);
        RespResult respResult = new RespResult();
        respResult.success(appointmentList);
        return respResult;
    }

    // 根据字段、排序方式、limit条 查询列表数据
    @RequestMapping("/list/limit")
    public RespResult selectListByLimit(@RequestParam(value = "field", required = false) String field,
                                        @RequestParam(value = "sort", required = false) String sort,
                                        @RequestParam(value = "limit", required = false) Integer limit) {
        List<Appointment> appointmentList = appointmentService.selectListByLimit(
                 field, sort, limit
        );
        RespResult respResult = new RespResult();
        respResult.success(appointmentList);
        return respResult;
    }

    // 根据字段查询列表数据
    @RequestMapping("/list/field")
    public RespResult selectListByField(@RequestParam(value = "field", required = false) String field,
                                        @RequestParam(value = "value", required = false) Object value) {
        List<Appointment> appointmentList = appointmentService.selectListByField(
                field, value
        );
        RespResult respResult = new RespResult();
        respResult.success(appointmentList);
        return respResult;
    }

    // 根据条件查询单个数据
    @RequestMapping("/info/dynamic")
    public RespResult infoDynamic(@RequestBody Appointment data) {
        Appointment appointment = appointmentService.selectOne(data);
        RespResult respResult = new RespResult();
        respResult.success(appointment);
        return respResult;
    }

    // 根据ID查询单个数据
    @RequestMapping("/info")
    public RespResult info(@RequestParam("id") Integer id) {
        Appointment appointment = appointmentService.selectByPrimaryKey(id);
        RespResult respResult = new RespResult();
        respResult.success(appointment);
        return respResult;
    }

    // 根据ID查询数据详情
    @RequestMapping("/details")
    public RespResult details(@RequestParam("id") Integer id) {
        Appointment appointment = appointmentService.selectOneByDetails(id);
        RespResult respResult = new RespResult();
        respResult.success(appointment);
        return respResult;
    }

    // 全部条数
    @RequestMapping("/count")
    public RespResult count() {
        int count = appointmentService.selectAllCount();
        RespResult respResult = new RespResult();
        respResult.success(count);
        return respResult;
    }

    // 根据条件查询数据条数
    @RequestMapping("/count/dynamic")
    public RespResult countDynamic(@RequestBody Appointment appointment) {
        int count = appointmentService.selectCount(appointment);
        RespResult respResult = new RespResult();
        respResult.success(count);
        return respResult;
    }

    // 根据条件修改全部数据
    @RequestMapping("/update/all")
    public RespResult updateAll(@RequestBody Appointment appointment) {
        appointmentService.updateAll(appointment);
        RespResult respResult = new RespResult();
        return respResult;
    }

    // 添加数据
    @RequestMapping("/add")
    public RespResult add(@RequestBody Appointment appointment) {
        appointment.setYear(DateUtils.getYear());
        appointment.setMonth(DateUtils.getMonth());
        appointment.setCreatDate(new Date());
        appointment.setNumber(FileUtils.createUUID());
        appointmentService.insertSelective(appointment);
        RespResult respResult = new RespResult();
        respResult.success(appointment);
        return respResult;
    }

    // 修改数据
    @RequestMapping("/edit")
    public RespResult edit(@RequestBody Appointment appointment) {
        if (appointment.getStatus() == 4) {
            Services services = servicesService.selectByPrimaryKey(appointment.getServicesId());
            // 服务完成，用户积分增加
            User user = userService.selectByPrimaryKey(appointment.getUserId());
            user.setIntegral(user.getIntegral() + services.getIntegral());
            userService.updateByPrimaryKeySelective(user);
        }
        appointmentService.updateByPrimaryKeySelective(appointment);
        RespResult respResult = new RespResult();
        respResult.success(appointment);
        return respResult;
    }

    // 单个删除
    @RequestMapping("/delete")
    public RespResult delete(@RequestParam("id") Integer id) {
        appointmentService.deleteByPrimaryKey(id);
        return new RespResult();
    }

    // 批量删除
    @RequestMapping("/delete/list")
    public RespResult deleteList(@RequestBody Integer[] idList) {
        for (Integer id : idList) {
            appointmentService.deleteByPrimaryKey(id);
        }
        return new RespResult();
    }

    // 年度预约量统计
    @RequestMapping("/statistics/year")
    public RespResult statisticsYear () {
        // 柱图数据
        List<String> nameList = new ArrayList<>();
        List<Object> valueList = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            int count = appointmentService.selectStatisticByYear(DateUtils.getYear(), i);
            nameList.add(i + "月");
            valueList.add(count);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("nameList", nameList);
        data.put("valueList", valueList);

        RespResult respResult = new RespResult();
        respResult.success(data);
        return respResult;
    }

}
